/*
Mif.Menu.List
*/

Mif.Menu.List=new Class({

	Implements: [Events, Options],
	
	options: {
		zIndex: 1,
		offsets:{x:0,y:0},
		morph: {link: 'cancel'},
		className: ''
	},

	initialize: function(structure, options){
		this.setOptions(options);
		$extend(this, structure);
		this.openChildList=null;
		this.zIndex=this.options.zIndex;
		this.className=this.options.className||this.owner.options.className;
		$extend(this.options.offsets, this.owner.options.offsets);
		this.items=[];
		this.groups={};
		this.selected=null;
		this.addEvent('draw', this.initEvents.bind(this));
		//this.container.setStyle('opacity',0);
		this.morpher=new Fx.Morph(this.container, this.options.morph);
		this.initEvents();
		
	},
	
	//private
	setWidth: function(){
		if(!Browser.Engine.trident) return;
		this.container.setStyle('width', 0);
		var width=this.list.offsetWidth;
		this.container.setStyle('width', width);
		this.list.setStyle('width',width);
		this.draw();
	},
	
	initEvents: function(){
		this.container.addEvents({
			'click': this.action.bind(this),
			'mousedown': $lambda(false),
			'selectstart': $lambda(false),
			'mouseover': this.initSelect.bind(this),
			'mouseout': this.initSelect.bind(this)
		});
	},
	
	action: function(){
		var current=this.selected;
		if(!current||current.disabled||current.owner.closing) return false;
		this.owner.hide();
		current.fireEvent('action');
		return false;
	},
	
	hide: function(parents){
		if(this.hiding) return;
		this.hiding=true;
		this.unselect();
		this.owner.showed.erase(this);
		/* this.morpher.start({opacity: 0}).chain(function(){
			this.container.dispose();
			this.owner.visible.erase(this);
			this.hiding=false;
			if(!this.owner.visible.length) this.owner.fireEvent('hide');
		}.bind(this)); */
		this.container.dispose();
		this.hideChildren();
		if(!parents) return;
		this.hideParents();
	},
	
	hideParents: function(){
		if(this.parentItem) this.parentItem.list.hide(true);
	},
	
	hideChildren: function(){
		var open=this.openChildList;
		if(!open) return;
		open.hide();
		this.openChildList=null;
	},
	
	show: function(coords){
		if(this.owner.showed.contains(this) && !this.hiding) return;
		this.owner.showed.push(this);
		this.owner.visible.push(this);
		this.draw();
		this.container.inject(document.body);
		//this.container.setStyle('width',300).setStyle('height',300);
		//this.setWidth();
		this.hiding=false;
		this.visible=true;
		this.position(coords);
		//this.morpher.start({opacity:1});
		
		this.showParents();
	},
	
	showParents: function(){
		if(!this.parentItem) return;
		this.parentItem.list.show();
	},
	
	initSelect: function(event){
		var target=event.target;
		if(!target||this.owner.closing) return this;
		if(target && Browser.Engine.trident){
			if(target.scopeName=='v') return this;
		}
		target=$(target);
		var item=target.getAncestorOrSelf('.mif-menu-item');
		
		if(!item){
			if(this.openChildList) return this;
			return this.unselect();
		};
		item=Mif.Menu.Items[item.getAttribute('uid')];
		if(!this.select(item)) return;
		if(!item.disabled && item.childList){
			item.timer=function(){
				item.childList.show();
				item.list.openChildList=item.childList;
				item.timer=null;
			}.delay(300);
		}
	},
	
	select: function(item){
		if(!item || item==this.selected || this.owner.closing) return false;
		this.unselect();
		item.select();
		this.show();
		this.selectParents(item);
		this.selected=item;
		this.owner.selected=item;
		return this;
	},
	
	selectParents: function(item){
		var parent=item.owner.parentItem;
		if(!parent) return;
		parent.owner.select(parent);
	},
	
	unselect: function(){
		var selected=this.selected;
		if(!selected) return;
		selected.unselect();
		if(selected.timer){
			$clear(selected.timer);
			selected.timer=null;
			return;
		}
		if(this.openChildList) this.openChildList.hide();
		this.openChildList=null;
		this.unselectChildren();
		this.selected=null;
		this.owner.selected=null;
	},
	
	unselectChildren: function(){
		var child=this.selected.childList;
		if(!child) return;
		child.unselect();
		child.hide();
	},
	
	position: function(coords){
		if(!coords){
		
			var parent=this.parentItem.container;
			position=parent.getPosition();
			
			var size = window.getSize(), scroll = window.getScroll();
			var menu = {x: this.container.offsetWidth, y: this.container.offsetHeight};
			var item = {x: parent.offsetWidth, y: 0};
			var props = {x: 'left', y: 'top'};
			var coords={};
			
			for (var z in props){
				var pos=position[z]+item[z]+this.options.offsets[z];
				if ((pos + menu[z] - scroll[z]) > size[z]) pos = position[z]-menu[z]-this.options.offsets[z];
				coords[z]=Math.max(0, pos);
			}
			
		}else if(coords.event){
		
			var size = window.getSize(), scroll = window.getScroll(), event=coords;
			var menu = {x: this.container.offsetWidth, y: this.container.offsetHeight};
			var props = {x: 'left', y: 'top'};
			var coords={};
			for (var z in props){
				var pos = event.page[z] + this.options.offsets[z];
				if ((pos + menu[z] - scroll[z]) > size[z]) pos = event.page[z] - this.options.offsets[z] - menu[z];
				coords[z]=Math.max(0, pos);
			}
			
		}
		
		return this.container.position(coords);
	}

});